package lumis.portal.presentation;

import java.util.List;

import lumis.portal.htmlevaluation.HtmlEvaluationMessage;
import lumis.portal.htmlevaluation.HtmlEvaluationUtil;
import lumis.portal.htmlevaluation.HtmlEvaluationMessage.Level;
import lumis.portal.manager.ManagerFactoryInternal;
import lumis.portal.page.PageWebResource;
import lumis.util.log.ILogger;
import lumis.util.log.LoggerFactory;

/**
 * Filter that allows evaluate the accessibility of a page from the request on
 * the Lumis Portal.
 * 
 * @version $Revision: 13093 $ $Date: 2011-05-28 18:40:18 -0300 (Sat, 28 May 2011) $
 * @since 6.0.0
 */
class HtmlEvaluator
{
	/**
	 * Class system Logger.
	 * @since 6.0.0
	 */
	private static final ILogger logger = LoggerFactory.getLogger(HtmlEvaluator.class);
	
	/**
	 * Logger used for html evaluation reports.
	 * @since 6.0.0
	 */
	private ILogger evaluationLogger = LoggerFactory.getServiceLogger("lumis.portal.presentation.htmlevaluation");

	/**
	 * Name of the Property Bag that will be contain the minimal level to be
	 * consider on the accessibility evaluation process.
	 * @since 6.0.0
	 */
	private static final String PROPERTY_HTML_EVALUATION_ENABLE = "lumis.portal.presentation.htmlevaluation.level";
	
	/**
	 * The evaluation level, or <code>null</code> if evaluation is disabled.
	 * @since 6.0.0
	 */
	private final Level level;
	
	/**
	 * URI displayed inside the evaluation report. For information only.
	 * @since 6.0.0
	 */
	private final String uri;

	public HtmlEvaluator(PageWebResource pageWebResource)
	{
		// calculates level to evaluate according to the property bag value
		List<String> propertyValues = pageWebResource.getProperties().get(PROPERTY_HTML_EVALUATION_ENABLE);
		if (propertyValues != null && !propertyValues.isEmpty())
		{
			Level parsedLevel;
			try
			{
				//Converts the value from property bag in Level enum.
				//if not occurs error of conversion the evaluation can be applied.
				String levelId = propertyValues.get(0).toUpperCase();
				parsedLevel = Level.valueOf(levelId);
			}
			catch (Exception e)
			{
				logger.error("Error getting Level from property bag value: " + propertyValues, e);
				parsedLevel = null;
			}
			
			level = parsedLevel;
		}
		else
		{
			level = null;
		}
		
		uri = pageWebResource.getPath();
	}
	
	/**
	 * Indicates whether this evaluator is enabled and will perform any
	 * evaluation.
	 * @return <code>true</code> if this evaluator is enabled, <code>false</code>
	 * otherwise.
	 * @since 6.0.0
	 */
	public boolean isEnabled()
	{
		return level != null;
	}
	
	/**
	 * Performs the evaluation of a HTML. A report of the result of the 
	 * evaluation is sent to the evaluation log.
	 * @param html the HTML to be evaluated.
	 * @since 6.0.0
	 */
	public void evaluate(String html)
	{
		if (isEnabled())
		{
			logger.debug("Evaluation for URI '" + uri + "' considered level: "+ level.name());
			
			// perform evaluation
			List<HtmlEvaluationMessage> messages = ManagerFactoryInternal.getHtmlEvaluationManager().evaluate(level, html);
			
			// log the result
			String logMsg = HtmlEvaluationUtil.formatOutput(messages, uri, html);
			evaluationLogger.info(logMsg);
		}
	}
}
